require "NodeManager"
require "WarScene"

GuanKaScene={}
local NodeID = {JuDianTable=2010, GuanKouTable=2011, GuanKouTableBK=2012, JuDianItemButton=2020}

GuanKaScene.judianData = {"stronghold1","stronghold2","stronghold3","stronghold4","stronghold5"}
GuanKaScene.guankouData = {"","","","","","","","","",""}

GuanKaScene.getNode = function()
  --if(GuanKaScene.node) then
  --  print("GuanKaScene.getNode return", GuanKaScene.node)
  --  return GuanKaScene.node
  --end
  GuanKaScene.node = CCBReader:load("YuanZhenHD.ccbi")  
  GuanKaScene.initJuDian()
  GuanKaScene.initGuanKou()
  --GuanKaScene.node:retain()
  return GuanKaScene.node
end

GuanKaScene.initGuanKou = function()
  local guankouTableBK = GuanKaScene.node:getChildByTag(NodeID.GuanKouTableBK)
  local size = guankouTableBK:getContentSize()
  local position = guankouTableBK:getPositionLua()
 
  local items = CCArray:create() 
  local count = #GuanKaScene.guankouData
  for i=0,count-1 do
    local guankouItem = CCBReader:load("JuDianItem.ccbi") 
    local s = guankouItem:getContentSize()
    --guankouItem:retain()
    items:addObject(guankouItem)
    local button = guankouItem:getChildByTag(NodeID.JuDianItemButton)
    NodeManager:addObject(string.format("judian-%d", i), button)
  end
  local tableview = CCTableView:create(0, CCSizeMake(size.width-20, size.height), items)
  local x, y = ActionUtil.leftBottomPoint(guankouTableBK)
  tableview:setPosition(x+10, y)
  GuanKaScene.node:addChild(tableview) 
end

GuanKaScene.initJuDian = function()
  local judianTable = GuanKaScene.node:getChildByTag(NodeID.JuDianTable)
  print("GuanKaScene.initJuDian", judianTable, NodeID.JuDianTable)
  judianTable = TypeCast(judianTable, "CCScrollView")
  local frameSize = judianTable:getViewSize()
  local count = #GuanKaScene.judianData;
  if count<=0 then
    return
  end
  local judianName = GuanKaScene.judianData[1]
  sp = CCSprite:create(string.format("%s.png", judianName))
  local itemwidth = sp:getTexture():getContentSizeInPixels().width
  --local itemwidth = 64
  local width = itemwidth * count;
 
  print("GuanKaScene.initJuDian", itemwidth, width, frameSize.height, frameSize.width)
  local layer = CCLayer:create()
  layer:setContentSize(CCSizeMake(width, frameSize.height))
    
  for i=0,count-1 do
    local judianName = GuanKaScene.judianData[i+1]
    local button = CCButton:createWithFileName("onTestButton", "UI74.png", "UI74.png", "UI74.png")
    button:setPosition(i*(itemwidth+10)+(itemwidth+10)/2, frameSize.height/2)
    local sp = CCSprite:create(string.format("%s.png", judianName))
    sp:setPosition(i*(itemwidth+10)+(itemwidth+10)/2, frameSize.height/2)
    local namesp = CCSprite:create(string.format("%sf.png", judianName))
    namesp:setPosition(i*(itemwidth+10)+(itemwidth+10)/2, frameSize.height/2)
     
    print("GuanKaScene.initJuDian", i*(itemwidth+5)+(itemwidth+5)/2, frameSize.height/2)
    layer:addChild(button)
    layer:addChild(sp)
    layer:addChild(namesp)
  end
  --layer.setPosition(240, 160)
  --GuanKaScene.node:addChild(layer)
  judianTable:setContainer(layer)
end

function guankaHandle(node)
  local name = NodeManager:getName(node) 
  local p = string.find(name, "-")
  local tmp = string.sub(name, p+1)
  local heroid = 70 + tonumber(tmp)
  print("guankaHandle", name, p, tmp, heroid)
  

  WarScene.defendHeroName = string.format("hero%d", heroid)
  local sc=WarScene.getScene()
  CCDirector:sharedDirector():replaceScene(CCTransitionSlideInL:create(1,sc))
  --local n = WarScene.getNode()
  --print("guankaHandle",n) 
  --local sc = CCDirector:sharedDirector():getRunningScene()
  --sc:addChild(WarScene.getNode())
end 
